//
//  SpecialViewController.m
//  Journey
//
//  Created by dllo on 16/5/19.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SpecialViewController.h"
#import "SpecialTableViewCell.h"
#import "SSNetworking.h"
#import "dataModel.h"
#import "SpecialDetailsViewController.h"
static NSString *const specialCell = @"SPECIALCELL";

@interface SpecialViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *SpecialTableView;
@property(nonatomic,strong)NSMutableArray *array;
@end

@implementation SpecialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"专题";
    self.array = [NSMutableArray array];
    [self getData];
    self.SpecialTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.SpecialTableView];
    self.SpecialTableView.delegate = self;
    self.SpecialTableView.dataSource = self;
    [self.SpecialTableView registerClass:[SpecialTableViewCell class] forCellReuseIdentifier:specialCell];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"return"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
  }

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SpecialDetailsViewController *specialDVC = [[SpecialDetailsViewController alloc] init];
    UINavigationController *specialNa = [[UINavigationController alloc] initWithRootViewController:specialDVC];
    dataModel *model = [self.array objectAtIndex:indexPath.row];
    specialDVC.specialArray = model.url;
    specialDVC.navigationItem.title = model.title;
    [self presentViewController:specialNa animated:YES completion:^{
    }];
}

-(void)getData{
    
    [SSNetworking ssGETWithURL:@"http://open.qyer.com/qyer/special/topic/special_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&page=1&track_app_channel=App%2520Store&track_app_version=6.8.5&track_device_info=iPhone%25205s&track_deviceid=E3EBA485-00FA-421D-BACB-D791AF3400E2&track_os=ios%25209.3.1&v=1" parameter:nil success:^(id result) {
        NSDictionary *dic = result;
        NSMutableArray *dataArray = [dic objectForKey:@"data"];
        for (NSDictionary *dataDic in dataArray) {
            dataModel *model = [[dataModel alloc] init];
            [model setValuesForKeysWithDictionary:dataDic];
            [self.array addObject:model];
        }
        NSLog(@"array = %@",self.array);
        [self.SpecialTableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    } network:^(BOOL networking) {
        
        
    }];
    
    }


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 300;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SpecialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:specialCell];
    dataModel *model = [self.array objectAtIndex:indexPath.row];
    cell.special = model;
    return cell;
}
-(void)leftButtonAction{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
@end
